package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.CheckGroup;
import com.xr.pojo.Member;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.CheckGroupService;
import com.xr.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("member")
public class MemberController {

    //    遠程訂閲服務 注解注入
    @Reference
    private MemberService memberService;

    //    查询所有会员info
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = memberService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, page);
    }

    //添加会员
    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.insertMember(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    //    根据id查询会员
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member byId = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //修改会员信息
    @RequestMapping("/update")
    public Result update(@RequestBody Member member) {
        try {
            memberService.update(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    //删除会员信息
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            memberService.delete(id);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.getMessage();
            return new Result(true,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }
}
